//
//  Location.swift
//  MeiLiTV
//
//  Created by 冰凉的枷锁 on 2017/3/31.
//  Copyright © 2017年 冰凉的枷锁. All rights reserved.
//

import Foundation
import UIKit

//class Location:UIViewController CLLocationManagerDelegate {
    /*
    func location() {
        locationManager = [CLLocationManager new];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager startUpdatingLocation];
    }
    #pragma mark -- CLLocationManagerDelegate
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    
    NSString *errorString;
    [manager stopUpdatingLocation];
    NSLog(@"Error: %@",[error localizedDescription]);
    switch([error code]) {
    case kCLErrorDenied:
    //Access denied by user
    errorString = @"用户关闭";
    //Do something...
    break;
    case kCLErrorLocationUnknown:
    //Probably temporary...
    errorString = @"位置数据不可用";
    //Do something else...
    break;
    default:
    errorString = @"未知错误";
    break;
    }
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:errorString delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
    [alert show];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(nonnull NSArray<CLLocation *> *)locations {
    
    CLLocation *currentLocation = locations.lastObject;
    
    if(currentLocation.horizontalAccuracy < 0) return;
    CLLocationCoordinate2D coordinate = currentLocation.coordinate;//位置坐标
    CGFloat longitude = coordinate.longitude;//经度
    CGFloat latitude = coordinate.latitude;//纬度
    
    [self.locationManager stopUpdatingLocation];
    
    //反地理编码
    CLGeocoder* geocoder = [[CLGeocoder alloc] init];
    CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
    NSLog(@"placemarks: %@", placemarks);
    CLPlacemark *placemark = [placemarks firstObject];
    self.addressTextField.text = placemark.name;
    NSLog(@"详细信息: %@",placemark.name);
    }];
    }
 */
//}
